﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace TagCloudSoma
{
    class WebController
    {
        public WebController() { }

        #region Public Methods
            public string GetSource(string uri)
            {
                WebRequest req = HttpWebRequest.Create(@uri);
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();

                req.Method = "GET";

                string source;
                Stream str = req.GetResponse().GetResponseStream();
                using (StreamReader reader = new StreamReader(str, Encoding.GetEncoding(response.CharacterSet), true))
                {
                    source = reader.ReadToEnd();
                }
                return source;
            }
            
            public string StripHTMLTag(string source)
            {
                Regex r;

                // Strip comments
                r = new Regex(@"<!--(\r?\n|.)*?-->");
                source = r.Replace(source, " ");

                // Strip google tags 
                r = new Regex(@"<script>(\r?\n|.)*?</script>");
                source = r.Replace(source, " ");

                // Strip javascript tags 
                r = new Regex(@"<(S|s)(C|c)(R|r)(I|i)(P|p)(T|t).*>(\r?\n|.)*?</(S|s)(C|c)(R|r)(I|i)(P|p)(T|t)>");
                source = r.Replace(source, " ");

                // Strip style tags
                r = new Regex(@"<(S|s)(T|t)(Y|y)(L|l)(E|e)>(\r?\n|.)*?</(S|s)(T|t)(Y|y)(L|l)(E|e)>");
                source = r.Replace(source, " ");

                // Strip html tags
                r = new Regex(@"<(.|\r?\n)*?>");
                source = r.Replace(source, " ");

                r = new Regex(@"\r?\n");
                source = r.Replace(source, " ");

                r = new Regex(@"&nbsp;");
                source = r.Replace(source, " ");

                r = new Regex(@"\s+");
                source = r.Replace(source, " ");

                r = new Regex(@"-");
                source = r.Replace(source, " ");

                return source.Trim();
            }
        #endregion
    }
}
